import pygame
import numpy as np
from moviepy.editor import VideoFileClip

# 初始化 Pygame
pygame.init()

# 设置屏幕大小
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))

# 打开视频文件
video_path = "example.mp4"  # 替换为你的视频文件路径
clip = VideoFileClip(video_path)

# 设置帧率
clock = pygame.time.Clock()

# 主循环
running = True
for frame in clip.iter_frames(fps=30, dtype='uint8'):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            break

    if not running:
        break

    # 将帧转换为 Pygame Surface (frame 是 (height, width, channels) 的数组)
    frame = np.rot90(frame)  # 转置数组，使其变为 (width, height, 3)
    frame = pygame.surfarray.make_surface(frame)

    # 显示视频帧
    screen.blit(frame, (0, 0))
    pygame.display.flip()

    # 控制帧率
    clock.tick(30)

# 释放资源
pygame.quit()
